﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Pages_ManagerPages_ManagerHome : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userManager"] == null)
        {
            Response.Redirect("ManagerLogin.aspx");
        }
        if (!IsPostBack)
        {
            lblUserManager.Text = Session["userManager"].ToString();
        }
    }
    private void getAllStudents()
    {
        ManagerBUS m = new ManagerBUS();
        DataSet ds = m.getAllStudent();
        if (ds.Tables[0].Rows.Count==0)
        {
            Response.Write("<script>alert('No Student')</script>");
        }
        else
        {
            grvStudent.DataSource = ds.Tables[0];
            grvStudent.DataBind();
        }
    }
    private void getAllStaffs()
    {
        ManagerBUS m = new ManagerBUS();
        DataSet ds = m.getAllStaff();
        if (ds.Tables[0].Rows.Count == 0)
        {
            Response.Write("<script>alert('No Staff')</script>");
        }
        else
        {
            grvStaff.DataSource = ds.Tables[0];
            grvStaff.DataBind();
        }
    }
    private void getAllCompetitions()
    {
        ManagerBUS manager = new ManagerBUS();
        DataSet ds = manager.getAllCompetition();
        if (ds== null)
        {
            Response.Write("<script>alert('No Competition')</script>");
        }
        else
        {
            grvCompetition.DataSource = ds.Tables[0];
            grvCompetition.DataBind();
        }
        
    }
    protected void grvStudent_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#ceedfc'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
            e.Row.Attributes.Add("style", "cursor:pointer;");
            e.Row.Attributes.Add("onclick", "location='updateStudent.aspx?id=" + e.Row.Cells[0].Text + "'");
        }
    }
    protected void grvCompetition_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#ceedfc'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
            e.Row.Attributes.Add("style", "cursor:pointer;");
            e.Row.Attributes.Add("onclick", "location='updateCompetition.aspx?id=" + e.Row.Cells[0].Text + "'");
        }
    }
    protected void grvStaff_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#ceedfc'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
            e.Row.Attributes.Add("style", "cursor:pointer;");
            e.Row.Attributes.Add("onclick", "location='updateStaff.aspx?id=" + e.Row.Cells[0].Text + "'");
        }
    }
    protected void btnSeachStudent_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "studentList", "displayBlock('studentList');", true);
        ManagerBUS m = new ManagerBUS();
        string name = txtSeachStudent.Text;
        if ("".Equals(name))
        {
            getAllStudents();
        }
        else {
            grvStudent.DataSource = m.searchStudentByName(name).Tables[0];
            grvStudent.DataBind();
        }

    }
    protected void btnSeachCompetition_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "competitionList", "displayBlock('competitionList');", true);
        ManagerBUS m = new ManagerBUS();
        string name = txtSeachCompetition.Text;
        if ("".Equals(name))
        {
            getAllCompetitions();
        }
        else
        {
            grvCompetition.DataSource = m.searchCompetition(name).Tables[0];
            grvCompetition.DataBind();
        }
    }
    protected void btnSeachStaff_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "staffList", "displayBlock('staffList');", true);
    }
    protected void btnDisplayAllStudent_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "studentList", "displayBlock('studentList');", true);
        getAllStudents();
    }
    protected void btnDisplayAllStaffs_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "staffList", "displayBlock('staffList');", true);
        getAllStaffs();
    }
    protected void btnDisplayAllCompetition_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "competitionList", "displayBlock('competitionList');", true);
        getAllCompetitions();
    }
    protected void btnAddStudent_Click(object sender, EventArgs e)
    {
        Student s = new Student();
        s.Id = -1;
        s.Username = txtUserName.Text;
        s.Password = txtPass.Text;
        s.Fullname = txtFullName.Text;
        s.Email = txtEmail.Text;
        s.PhoneNumber = txtPhone.Text;
        s.Major = txtMajor.Text;
        s._class1 = txtClass.Text;
        s.Address = txtAddress.Text;
        ManagerBUS manager = new ManagerBUS();
        if (manager.insertStudent(s))
        {
            Response.Write("<script>alert('Success')</script>");
            Response.Redirect("ManagerHome.aspx");
        }
        else
        {
            Response.Write("<script>alert('Failed')</script>");
            Response.Redirect("ManagerHome.aspx");
        }
    }
    protected void btnAddStaff_Click(object sender, EventArgs e)
    {
        Staff s = new Staff();
        s.Id = -1;
        s.Username = txtUserNameStaff.Text;
        s.Password = txtPassStaff.Text;
        s.Fullname = txtFullNameStaff.Text;
        s.Email = txtEmailStaff.Text;
        s.PhoneNumber = txtPhoneStaff.Text;
        s.Major = txtMajorStaff.Text;
        s.Address = txtAddressStaff.Text;
        ManagerBUS manager = new ManagerBUS();
        if (manager.insertStaff(s))
        {
            Response.Write("<script>alert('Success')</script>");
            Response.Redirect("ManagerHome.aspx");
        }
        else
        {
            Response.Write("<script>alert('Failed')</script>");
            Response.Redirect("ManagerHome.aspx");
        }
    }
}